package com.tang.imgupload.util;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Component
public class FastDFSUtil {
	@Resource
	FastFileStorageClient storageClient;

	/**
	 * MultipartFile类型的文件上传ַ
	 *
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public String uploadFile(MultipartFile file) throws IOException{
		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
		                                               FilenameUtils.getExtension(file.getOriginalFilename()), null);
		return storePath.getFullPath();
	}

	/**
	 * 删除文件
	 *
	 * @param path 全路径，包含扩展名
	 */
	public void deleteFile(String path){
		storageClient.deleteFile(path);
	}
}
